# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.utils import timezone


# Create your models here.

class User (models.Model):
    username = models.CharField(max_length=30)
    password = models.CharField(max_length=100)
    email = models.CharField(max_length=30, null=True)
    phone = models.CharField(max_length=30, null=True)
    grade = models.CharField(max_length=30, null=True)
    role = models.IntegerField(default=0)  # 1管理员/0普通用户
    create_time = models.DateTimeField(null=True, auto_now_add=True)
    update_time = models.DateTimeField(null=True, default=timezone.now)
    user_profile = models.ImageField(null=True)


class Category (models.Model):
    name = models.CharField(max_length=100)
    introduction = models.CharField(max_length=300, null=True)

    def __str__(self):
        return self.name


class Brand (models.Model):
    name = models.CharField(max_length=100)
    introduction = models.CharField(max_length=300, null=True)

    def __str__(self):
        return self.name


class Spu (models.Model):
    name = models.CharField(max_length=200)
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, null=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    introduction = models.CharField(max_length=500, null=True)
    create_time = models.DateTimeField(null=True, auto_now_add=True)
    update_time = models.DateTimeField(null=True, default=timezone.now)
    picture = models.ImageField(null=True)  # 主图片


class Sku (models.Model):
    # spu = models.ForeignKey(Spu,on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, null=True)
    brand_name = models.CharField(max_length=100, null=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    category_name = models.CharField(max_length=100, null=True)
    introduction = models.CharField(max_length=500, null=True)
    picture = models.ImageField(null=True)  # 主图片

    old_price = models.FloatField(null=True)
    new_price = models.FloatField(null=True)
    color = models.CharField(max_length=50, null=True)
    type = models.CharField(max_length=500, null=True)  # 型号
    stock = models.IntegerField(null=True)  # 库存
    sales_volume = models.IntegerField(default=0)  # 销量
    status = models.IntegerField()  # 0-未上架 1-上架中 2-热卖 3-缺货
    create_time = models.DateTimeField(null=True, auto_now_add=True)
    update_time = models.DateTimeField(null=True, default=timezone.now)


class Sku_images (models.Model):
    sku = models.ForeignKey(Sku, on_delete=models.CASCADE)
    image = models.ImageField()


class Cart (models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    sku = models.ForeignKey(Sku, on_delete=models.DO_NOTHING)
    quantity = models.IntegerField()
    checked = models.IntegerField(default=2)  # 是否选择 1=已勾选 2=未勾选
    create_time = models.DateTimeField(null=True, auto_now_add=True)
    update_time = models.DateTimeField(null=True, default=timezone.now)


class Address (models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=30)
    province = models.CharField(max_length=50)
    city = models.CharField(max_length=50)
    county = models.CharField(max_length=50)
    address = models.CharField(max_length=200)
    postcode = models.CharField(max_length=20, null=True)
    create_time = models.DateTimeField(null=True, auto_now_add=True)
    update_time = models.DateTimeField(null=True, default=timezone.now)


class Order (models.Model):
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    address = models.ForeignKey(Address, on_delete=models.DO_NOTHING)
    payment = models.FloatField()  # 实际付款金额
    pay_type = models.IntegerField(default=1)  # 1:在线支付 2：货到付款
    pay_platform = models.IntegerField()  # 1:支付宝 2:微信 3：其他
    postage = models.FloatField(default=0)  # 运费
    status = models.IntegerField()  # 0-已取消 1-未付款 2-已付款 3-已发货 4-交易成功 5-交易失败

    name = models.CharField(max_length=50,null=True)
    phone = models.CharField(max_length=20,null=True)
    address2 = models.CharField(max_length=20,null=True)
    note = models.CharField(max_length=300,null=True)  # 备注

    pay_time = models.DateTimeField(null=True)
    send_time = models.DateTimeField(null=True)
    end_time = models.DateTimeField(null=True)  # 交易完成时间
    close_time = models.DateTimeField(null=True)  # 交易关闭时间
    create_time = models.DateTimeField(null=True, auto_now_add=True)
    update_time = models.DateTimeField(null=True, default=timezone.now)


class OrderInfo (models.Model):
    order = models.ForeignKey(Order, on_delete=models.DO_NOTHING)
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    sku = models.ForeignKey(Sku, on_delete=models.DO_NOTHING)
    sku_name = models.CharField(max_length=100)
    sku_image = models.ImageField(null=True)
    unit_price = models.FloatField()
    quantity = models.IntegerField()
    create_time = models.DateTimeField(null=True, auto_now_add=True)
    update_time = models.DateTimeField(null=True, default=timezone.now)


class Comment (models.Model):
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    order = models.ForeignKey(Order, on_delete=models.DO_NOTHING)
    rate = models.IntegerField()  # 评分1-5
    comment = models.CharField(max_length=500)
    create_time = models.DateTimeField(null=True, auto_now_add=True)
    update_time = models.DateTimeField(null=True, default=timezone.now)

